import 'package:card_topia/api/api_path.dart';
import 'package:card_topia/api/network_util.dart';
import 'package:card_topia/common_provider/page_provider.dart';
import 'package:card_topia/common_provider/user_provider.dart';
import 'package:card_topia/common_widget/my_app_bar.dart';
import 'package:card_topia/common_widget/my_scaffold.dart';
import 'package:card_topia/design_specifications.dart';
import 'package:card_topia/main.dart';
import 'package:card_topia/providers/code_provider.dart';
import 'package:card_topia/service/common_service.dart';
import 'package:card_topia/service/login_service.dart';
import 'package:card_topia/utils/dialog_util.dart';
import 'package:card_topia/utils/event_util.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:pin_code_fields/pin_code_fields.dart';

import '../../my_router.dart';

///验证验证码页面



class VerifyCodePage extends ConsumerStatefulWidget {
  const VerifyCodePage({super.key, required this.email});
  final String email;
  @override
  ConsumerState<VerifyCodePage> createState() => _VerifyCodePageState();
}

class _VerifyCodePageState extends ConsumerState<VerifyCodePage> {
  final TextEditingController _controller = TextEditingController();
  late final pagePod = ref.read(pageProvider);
  _loginByCode()async{
    DialogUtil.showLoading();
    final result = await LoginService.loginByCode(email: widget.email, code: _controller.text);
    if(result.success){
      final success = await ref.read(userInfoProvider).setUserInfo(result.data);
      if(success){
        if(context.mounted){
          EventUtil.eventBus.fire(TabChangeEvent(pagePod.aimPage));
          // context.pushReplacement("/");
          MyRouter.push(page: MyHomePage());

        }
      }
      DialogUtil.dismiss();
    }else{
      EasyLoading.showToast("${result.msg}");
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: const CustomAppBar(showLeading: true,),
      body: Column(
        children: [
          SizedBox(height: 24.w,),
          Row(
            children: [
              SizedBox(width: 16.w,),
              Text("验证码已发送至",style: TextStyle(fontFamily: harmony,fontSize: 18.sp,color: textColor,fontWeight: FontWeight.w400),),
              Text(" ${widget.email}",style: TextStyle(fontFamily: harmony,fontSize: 18.sp,color: themeColor),),
            ],
          ),
          SizedBox(height: 32.w,),
          Container(
            padding: EdgeInsets.symmetric(horizontal: 32.w),
            child: PinCodeTextField(
              length: 6, appContext: context,
              cursorColor: themeColor,
              keyboardType: TextInputType.number,
              inputFormatters: [
                FilteringTextInputFormatter.digitsOnly
              ],
              animationType: AnimationType.scale,
              controller: _controller,
              autoFocus: true,
              pinTheme: PinTheme(
                selectedColor: themeColor,
                inactiveColor: disableColor,
                activeColor: themeColor
              ),
              onCompleted: (value){
                _loginByCode();
              },
            ),
          ),
          // SizedBox(height: 16.w,),
          Consumer(builder: (context,ref,child){
            final pod = ref.watch(codeProvider);
            return InkWell(
              onTap: ()async{
                DialogUtil.showLoading();
                // final result = await pod.sendEmailCode(context,email: widget.email);
                final result = await CommonService.sendEmailCode(email: widget.email);
                DialogUtil.dismiss();
                if(result){
                  ref.read(codeProvider).startCountDown();
                }
              },
              child: Container(
                alignment: Alignment.centerRight,
                padding: EdgeInsets.only(right:32.w,top: 16.w),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.end,
                  children: [
                    Text("重新发送",style: TextStyle(fontSize: 12.sp,fontFamily: harmony,color: pod.counter==60?textColor:disableColor),),
                    pod.counter==60?Container():SizedBox(
                        width: 30.w,
                        child: Text("(${ref.watch(codeProvider).counter})",style: TextStyle(fontSize: 12.sp,fontFamily: harmony,color: pod.counter==60?textColor:disableColor),)),
                  ],
                ),
              ),
            );
          })
          // Container(
          //   margin: EdgeInsets.symmetric(horizontal: 16.w),
          //   child: LoadingButton(
          //       text: "登录", loadingText: '', onTap: _loginByCode, enable: _enable),
          // ),
        ],
      ),
    ).withOnTap(context);
  }
}
